package com.example.config;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import com.example.handler.MyHandshakeInterceptor;
import com.example.handler.SnakeWebSocketHandler;
import com.example.handler.WebSocketHandler;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource
    private WebSocketHandler handler;

    @Resource
    private SnakeWebSocketHandler snakeWebSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(handler, "/echo")
        // 添加拦截器
                .addInterceptors(new MyHandshakeInterceptor()).withSockJS();
        registry.addHandler(snakeWebSocketHandler, "/snake").withSockJS();
        registry.addHandler(handler, "/sockjs/message").setAllowedOrigins("*")
                .withSockJS();
    }

}
